//
//  LGTRWZTaskViewCell.swift
//  KJLGT
//
//  Created by jenkin on 2019/8/20.
//  Copyright © 2019年 jenkin. All rights reserved.
//

import UIKit

class LGTRWZTaskViewCell: UITableViewCell {

    var moneyLabel:UILabel?
    var titleLabel:UILabel?
    var timeLabel:UILabel?
    var stateLabel:UILabel?
    var leastLabel:UILabel?
    var _model:TaskModel?
    var model:TaskModel?{
        get{
            return _model
        }set{
            _model = newValue
            self.setData()
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = UITableViewCell.SelectionStyle.none
        let imageView:UIImageView = LGTViewCreateUtil.share.createImageView(image: "earn")
        self.addSubview(imageView)
        imageView.snp.makeConstraints { (make) in
            make.left.equalTo(SizeFit(9))
            make.top.equalTo(11)
            make.height.equalTo(SizeFit(49))
            make.width.equalTo(SizeFit(51))
            make.bottom.equalTo(-SizeFit(9))
        }
        
        self.moneyLabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.center, textColor: RedColor, fontSize: SizeFitFloat(19), text: "")
        self.addSubview(self.moneyLabel!)
        self.moneyLabel?.snp.makeConstraints({ (make) in
            make.center.equalTo(imageView)
            make.height.equalTo(SizeFitFloat(19))
            make.width.equalTo(SizeFit(51))
        })
        
        let line = LGTViewCreateUtil.share.createLineView()
        self.addSubview(line)
        line.snp.makeConstraints { (make) in
            make.left.equalTo(SizeFit(9))
            make.right.equalTo(-SizeFit(9))
            make.height.equalTo(1)
            make.bottom.equalTo(0)
        }
        
        self.titleLabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: TextColor, fontSize: SizeFitFloat(14), text: "")
        self.addSubview(self.titleLabel!)
        self.titleLabel?.snp.makeConstraints({ (make) in
            make.left.equalTo(imageView.snp_rightMargin).offset(SizeFit(12))
            make.height.equalTo(SizeFit(12))
            make.width.equalTo(SizeFit(200))
            make.top.equalTo(SizeFit(17))
        })
        
        self.timeLabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: TextColor, fontSize: SizeFitFloat(10), text: "")
        self.addSubview(self.timeLabel!)
        self.timeLabel?.snp.makeConstraints({ (make) in
            make.left.equalTo(imageView.snp_rightMargin).offset(SizeFit(12))
            make.height.equalTo(SizeFit(10))
            make.width.equalTo(SizeFit(200))
            make.bottom.equalTo(imageView).offset(-SizeFit(3))
        })
        
        
        //FF1A1A  46B25F 999999 FFB21A 999999 333333 3BB1EE
        self.stateLabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.right, textColor: TextColor, fontSize: SizeFitFloat(15), text: "")
        self.addSubview(self.stateLabel!)
        self.stateLabel?.snp.makeConstraints({ (make) in
            make.right.equalTo(SizeFitFloat(-9))
            make.height.equalTo(SizeFit(12))
            make.centerY.equalTo(self.titleLabel!)
        })
        
        self.leastLabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.right, textColor: TextColor, fontSize: SizeFitFloat(10), text: "")
        self.addSubview(self.leastLabel!)
        self.leastLabel?.snp.makeConstraints({ (make) in
            make.right.equalTo(SizeFitFloat(-9))
            make.height.equalTo(SizeFit(10))
            make.centerY.equalTo(self.timeLabel!)
        })
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setData(){
        self.moneyLabel?.text = "\(String(describing: self.model!.price!))"
        self.titleLabel?.text = self.model?.title
        self.timeLabel?.text = self.model?.update_time
        
        let key:String = "\(self.model!.status!)"
        let json:Dictionary<String, Any>? = TaskStateInfo[key] as? Dictionary<String, Any> ?? Dictionary<String, Any>.init()
        let stateStr:String = json?["title"] as? String ?? ""
        let desc:String = json?["desc"] as? String ?? ""
        let color:UIColor = json?["color"] as? UIColor ?? UIColor.clear
        let timeDesc:String = json?["timeDesc"] as? String ?? ""
        // status (integer, optional): 状态[1-审核中,2-未通过,3-派发中,4-暂停中,5-已撤回,6-已过期,7-已完成] ,
        //    var free_count:Int? = 0 //(integer, optional): 空闲数 ,
        // var success_count:Int? = 0 // (integer, optional): 完成数 ,
        // total_count 总数
        //完成、撤回->显示完成数  未通过、审核中->显示总数 剩余显示 -> 空闲数
        var count = self.model?.free_count
        if self.model!.status == 7 || self.model?.status == 5{
            count = self.model?.success_count
        }else if self.model!.status == 1 || self.model?.status == 2{
             count = self.model?.total_count
        }
        
        self.stateLabel?.text = "\(stateStr)"
        self.timeLabel?.text = "\(timeDesc)\(String(describing: self.model!.update_time!))"
        self.leastLabel?.text = "\(desc)\(String(describing: count!))次"
        self.stateLabel?.textColor = color
    }
}
